package moni.anyou.com.view.view.my;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import moni.anyou.com.view.R;
import moni.anyou.com.view.base.BaseActivity;
import moni.anyou.com.view.bean.request.ReqUpdateChildBaseInfoBean;
import moni.anyou.com.view.config.SysConfig;
import moni.anyou.com.view.tool.AppTools;
import moni.anyou.com.view.widget.NetProgressWindowDialog;

import org.json.JSONObject;
import org.kymjs.aframe.http.KJHttp;
import org.kymjs.aframe.http.KJStringParams;
import org.kymjs.aframe.http.StringCallBack;

/**
 * Created by Administrator on 2017/5/31.
 */

public class EditUserInfoActivity extends BaseActivity implements View.OnClickListener {
    private static String TYPE_SEX = "sex";
    private static String TYPE_BIRTHDAY = "birthday";
    private static String TYPE_ROLE = "role";
    private static String TYPE_ICON = "icon";
    private static String TYPE_CONTETS = "contents"; //老师寄语
    private EditText etUnfo;
    private NetProgressWindowDialog window;
    private String strType = "";
    private String strTypeName = "";
    private String strValue = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editinfo);
        init();
    }

    @Override
    public void initView() {
        super.initView();
        initTitle();
        window = new NetProgressWindowDialog(mBaseActivity);
        Intent intent = getIntent();
        strType = intent.getStringExtra("type");
        strTypeName = intent.getStringExtra("typename");
        strValue = intent.getStringExtra("value");

        tvTitle.setText("修改"+strTypeName);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("完成");
        etUnfo = (EditText) findViewById(R.id.et_info);
        etUnfo.setText(strValue);
    }

    @Override
    public void setAction() {
        super.setAction();
        ivBack.setOnClickListener(this);
        tvRight.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_left:
                onBack();
                break;
            case R.id.right_tv:
                postUpdateInfo();
                break;
        }
    }


    public void postUpdateInfo() {
        KJHttp kjh = new KJHttp();
        KJStringParams params = new KJStringParams();
        strValue =  etUnfo.getText().toString();
        String cmdPara = new ReqUpdateChildBaseInfoBean("6", SysConfig.uid, SysConfig.token, strType, strValue ).ToJsonString();
        params.put("sendMsg", cmdPara);
        window.ShowWindow();
        kjh.urlGet(SysConfig.ServerUrl, params, new StringCallBack() {
            @Override
            public void onSuccess(String t) {

                Log.d(TAG, "onSuccess: " + t);
                try {
                    JSONObject jsonObject = new JSONObject(t);
                    int result = Integer.parseInt(jsonObject.getString("result"));
                    if (result >= 1) {
                        SharedPreferences.Editor editor = SysConfig.prefs.edit();// 取得编辑器
                        SysConfig.userInfoJson.put(strType, strValue);
                        /*
                        if (strType.equals(TYPE_ICON)) {
                            SysConfig.userInfoJson.put("icon",  strValue);
                            // setBitmaptoImageView11(SysConfig.PicUrl +"/" + mVaule, tvHeadIcon);
                        } else if (strType == TYPE_BIRTHDAY) {
                            SysConfig.userInfoJson.put("birthday",strValue);
                        } else if (strType == TYPE_CONTETS) {
                            SysConfig.userInfoJson.put("contents", strValue);
                        } else if (strType == TYPE_SEX) {
                            SysConfig.userInfoJson.put("sex", strValue);
                        } else if (strType == TYPE_ROLE) {
                            SysConfig.userInfoJson.put("role", strValue);
                        }*/
                        editor.putString("sUserInfoJson", SysConfig.userInfoJson.toString());
                        editor.commit();// 提交刷新数据

                        Toast.makeText(mContext, "修改成功", Toast.LENGTH_SHORT).show();
                        EditUserInfoActivity.this.finish();
                    } else {
                        if (result == -2) {
                            AppTools.jumptoLogin(mBaseActivity);
                        }
                        else {
                            Toast.makeText(mContext, jsonObject.get("retmsg").toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                } catch (Exception ex) {
                    Toast.makeText(mContext, "数据请求失败", Toast.LENGTH_SHORT).show();

                }
                window.closeWindow();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Toast.makeText(mContext, "网络异常，请稍后再试", Toast.LENGTH_SHORT).show();

                window.closeWindow();
            }
        });
    }



}
